package com.je.app.view.popUp;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.SystemColor;

import javax.swing.JDialog;
import javax.swing.UIManager;

import com.je.app.control.ControlCaja;


import com.je.app.view.MainFrame;
import com.je.app.view.MainFrame.MessageType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class AgregarMovimiento extends JDialog{
	protected JTextField fieldMonto;
	protected JTextField fieldDetalle;
	protected JButton botonAceptar;
	protected JButton botonCancelar;
	
	public AgregarMovimiento (final String tipoMovimiento) {
		
		setSize(new Dimension(480, 204));		
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
		setTitle("Crear movimiento");
		
		JLabel lblDetalle = new JLabel("Detalle:");
		lblDetalle.setHorizontalAlignment(SwingConstants.LEFT);
		lblDetalle.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblDetalle.setBounds(29, 74, 60, 26);
		getContentPane().add(lblDetalle);
		
		JLabel lblSaldo = new JLabel("Monto:");
		lblSaldo.setHorizontalAlignment(SwingConstants.LEFT);
		lblSaldo.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblSaldo.setBounds(29, 27, 60, 26);
		getContentPane().add(lblSaldo);
		
		fieldMonto = new JTextField();
		fieldMonto.setHorizontalAlignment(SwingConstants.LEFT);
		fieldMonto.setFont(new Font("Tahoma", Font.PLAIN, 12));
		fieldMonto.setColumns(10);
		fieldMonto.setBounds(111, 27, 84, 26);
		getContentPane().add(fieldMonto);
		
		botonAceptar = new JButton("Aceptar");
		botonAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				double monto=0;
				boolean validado=true;
				String detalle=null;
				try{
					monto = new Double(fieldMonto.getText());
				}catch(Exception e){
					validado=false;
					MainFrame.getInstance().showMessage(MessageType.ERROR, "El monto ingresado no es v\u00E1lido");
				}
				
				try{
					detalle = fieldDetalle.getText();
					if(detalle.length()<3 || detalle.length()>20) throw new Exception();
				}catch(Exception e){
					validado=false;
					MainFrame.getInstance().showMessage(MessageType.ERROR, "El detalle debe tener entre 3 y 20 caracteres");
				}
				

				if(validado){
					ControlCaja.getInstance().crearMovimiento(tipoMovimiento, monto, detalle, "   ");
					dispose();
				}
				
			}
		});
		botonAceptar.setForeground(Color.BLACK);
		botonAceptar.setFont(new Font("Tahoma", Font.PLAIN, 12));
		botonAceptar.setBorder(UIManager.getBorder("Button.border"));
		botonAceptar.setBackground(SystemColor.activeCaptionBorder);
		botonAceptar.setBounds(111, 122, 84, 26);
		getContentPane().add(botonAceptar);
		
		botonCancelar = new JButton("Cancelar");
		botonCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		botonCancelar.setForeground(Color.BLACK);
		botonCancelar.setFont(new Font("Tahoma", Font.PLAIN, 12));
		botonCancelar.setBorder(UIManager.getBorder("Button.border"));
		botonCancelar.setBackground(SystemColor.activeCaptionBorder);
		botonCancelar.setBounds(267, 122, 84, 26);
		getContentPane().add(botonCancelar);
		
		fieldDetalle = new JTextField();
		fieldDetalle.setHorizontalAlignment(SwingConstants.LEFT);
		fieldDetalle.setFont(new Font("Tahoma", Font.PLAIN, 12));
		fieldDetalle.setColumns(10);
		fieldDetalle.setBounds(111, 74, 240, 26);
		getContentPane().add(fieldDetalle);
		setVisible(true);
		
	}
}
